
package com.jf.cloud.supplier.service.impl;

import com.jf.cloud.api.supplier.vo.SupplierApiRefundAddrVO;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.supplier.dto.SupplierRefundAddrDTO;
import com.jf.cloud.supplier.mapper.SupplierRefundAddrMapper;
import com.jf.cloud.supplier.model.SupplierRefundAddr;
import com.jf.cloud.supplier.service.SupplierRefundAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 供应商退货地址
 *
 * @author zz
 * @date 2022-03-14 17:04:32
 */
@Service
public class SupplierRefundAddrServiceImpl implements SupplierRefundAddrService {

    @Autowired
    private SupplierRefundAddrMapper supplierRefundAddrMapper;

    @Override
    public PageVO<SupplierApiRefundAddrVO> page(PageDTO pageDTO, SupplierRefundAddrDTO supplierRefundAddrDTO) {
        return PageUtil.doPage(pageDTO, () -> supplierRefundAddrMapper.list(supplierRefundAddrDTO));
    }

    @Override
    public SupplierApiRefundAddrVO getBySupplierRefundAddrId(Long supplierRefundAddrId, Long supplierId) {
        return supplierRefundAddrMapper.getBySupplierRefundAddrId(supplierRefundAddrId, supplierId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SupplierRefundAddr supplierRefundAddr) {
        if (Objects.equals(supplierRefundAddr.getIsDefault(), 1)) {
            supplierRefundAddrMapper.cancelDefaultAddr(supplierRefundAddr.getSupplierId());
        }
        supplierRefundAddrMapper.save(supplierRefundAddr);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SupplierRefundAddr supplierRefundAddr) {
        SupplierApiRefundAddrVO dbSupplierRefund = getBySupplierRefundAddrId(supplierRefundAddr.getSupplierRefundAddrId(), supplierRefundAddr.getSupplierId());
        if (Objects.isNull(dbSupplierRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(supplierRefundAddr.getSupplierId(), dbSupplierRefund.getSupplierId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        if (Objects.equals(supplierRefundAddr.getIsDefault(), 1) && Objects.equals(dbSupplierRefund.getIsDefault(), 0)) {
            supplierRefundAddrMapper.cancelDefaultAddr(supplierRefundAddr.getSupplierId());
        }
        supplierRefundAddrMapper.update(supplierRefundAddr);
    }

    @Override
    public void deleteById(Long supplierRefundAddrId, Long supplierId) {
        SupplierApiRefundAddrVO dbSupplierRefund = getBySupplierRefundAddrId(supplierRefundAddrId, supplierId);
        if (Objects.isNull(dbSupplierRefund)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        } else if (!Objects.equals(dbSupplierRefund.getSupplierId(), supplierId)) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        supplierRefundAddrMapper.deleteById(supplierRefundAddrId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.SUPPLIER_REFUND_ADDR, key = "#supplierId", sync = true)
    public List<SupplierApiRefundAddrVO> listBySupplierId(Long supplierId) {
        return supplierRefundAddrMapper.listBySupplierId(supplierId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SUPPLIER_REFUND_ADDR, key = "#supplierId")
    public void removeCacheBySupplierId(Long supplierId) {
    }
}
